package cn.wowjoy.pda.android_pharmacy.ui.activity.staticH.check;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityStaticCheckResultDetailBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPatientBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.staticH.StaticCheckResultDetailPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.staticH.StaticCheckResultDetailViewModel;

public class StaticCheckResultDetailActivity extends BaseActivity<ActivityStaticCheckResultDetailBinding,StaticCheckResultDetailViewModel,StaticCheckResultDetailPresenter> {
    private MDialog mDialog;
    InHospitalPatientBeen topRemove ;
    public static final int CLOSE=1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CLOSE:
                    //TODO:3秒后自动跳转
                    if(null != mDialog){
                        mDialog.dismiss();
                        Intent intent=new Intent();
                        intent.putExtra("check",topRemove);
                        setResult(Contants.RESULT_SUCCESS_CODE,intent);
                        finish();
                    }
                    break;
            }
        }
    };


    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setViewModel(viewModel);
        binding.setPresenter(presenter);
        binding.hosptalResultTitle.titleTextTv.setText("药品审核");
        //TODO:界面信息的更细
        topRemove = (InHospitalPatientBeen) getIntent().getExtras().getSerializable("check");
//        Log.e("topRemove", "init: "+ topRemove.toString() );
        if(null != topRemove){
//            updateUI(topRemove);
            binding.setModel(topRemove);
            if(null != topRemove.getSFGWPB() && !"".equals(topRemove.getSFGWPB())){
                if(Integer.parseInt(topRemove.getSFGWPB()) == 1){
                    binding.medicineNameHosptalResult.setTextColor(getResources().getColor(R.color.red));
                }else{
                    binding.medicineNameHosptalResult.setTextColor(getResources().getColor(R.color.text_undanger));
                }
            }
            binding.medicineNameHosptalResult.setText(topRemove.getZZYPMC()+"   "+topRemove.getZZYPGG());
            binding.numFactHosptalResult.setText(topRemove.getCFYPSL());
            binding.numFactHosptalResult.setSelection( binding.numFactHosptalResult.getText().length());
        }
        //TODO:应取数量和取药数量的对比
        binding.btSureHosptalResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.numFactHosptalResult.getText().length() == 0) {
                    new MyToast(StaticCheckResultDetailActivity.this).showinfo("请输入取药数量");
                } else {
                    if (presenter.check(binding.numShouldHosptalResult.getText().toString().trim(), binding.numFactHosptalResult.getText().toString().trim())) {
                        //TODO:弹框 药品Name
                        mDialog = CreateDialog.successDialog(StaticCheckResultDetailActivity.this,6 ,"审核成功");
                        mHandler.sendEmptyMessageDelayed(CLOSE,600);
                    }else{
                        binding.numFactHosptalResult.setText("");
                    }
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent=new Intent();
        intent.putExtra("check",topRemove);
        setResult(Contants.RESULT_FAIL_CODE ,intent);
        super.onBackPressed();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_static_check_result_detail;
    }

    @Override
    protected Class<StaticCheckResultDetailPresenter> getPresenterClass() {
        return StaticCheckResultDetailPresenter.class;
    }

    @Override
    protected Class<StaticCheckResultDetailViewModel> getViewModelClass() {
        return StaticCheckResultDetailViewModel.class;
    }
}
